import requests
from util import debug

class Fetcher:
    """
    封装 requests, 并提供返回页面 html 代码
    """
    def __init__(self, url_prefix):
        self.url_prefix = url_prefix

    def _get_header(self):
        # 获取header
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36',
            'Host': 'ts.lianjia.com',
            'Upgrade-Insecure-Requests': '1',
            'Sec-Fetch-User': '?1',
            'Sec-Fetch-Dest': 'document',
            'Sec-Fetch-Mode': 'navigate',
            'Sec-Fetch-Site': 'none',
            'Cookie': ''
        }
        # debug(headers, '生成的header')
        return headers

    def get_content(self, keyword, page):
        """
        访问形如 url/{keyword}/pg{page}
        :param keyword: 参数
        :param page: 分页页码
        :return: 页面代码
        """
        headers = self._get_header()
        resp = requests.get(url=f"{self.url_prefix}/{keyword}/pg{page}", headers=headers)
        html = resp.content.decode()
        return html

    def get_content2(self, keyword, page):
        """
        访问形如 url/pg{page}c{keyword}/
        :param keyword: 参数
        :param page: 分页页码
        :return: 页面代码
        """
        headers = self._get_header()
        url = f"{self.url_prefix}/pg{page}c{keyword}/"
        debug(url, "生成的url")
        resp = requests.get(url=url, headers=headers)
        html = resp.content.decode()
        return html

    def get_content3(self, keyword):
        """
        访问形如 url/{keyword}.html"
        :param keyword: 参数
        :return: 页面代码
        """
        headers = self._get_header()
        url = f"{self.url_prefix}/{keyword}.html"
        debug(url, "生成的url")
        resp = requests.get(url=url, headers=headers)
        html = resp.content.decode()
        return html

    def get_content4(self, keyword):
        """
        访问形如 url/{keyword}
        :param keyword: 参数
        :return: 页面代码
        """
        headers = self._get_header()
        url = f"{self.url_prefix}/{keyword}"
        debug(url, "生成的url")
        resp = requests.get(url=url, headers=headers)
        html = resp.content.decode()
        return html
